var captcha={};
//倒计时60
var countdown = 60;
function settime(obj) {
    if (countdown == 0) {
        obj.removeAttribute("disabled");
        obj.value = "获取验证码";
        countdown = 60;
        return;
    } else {
        obj.setAttribute("disabled", true);
        obj.value = "" + countdown + "秒后重新发送";
        countdown--;
    }
    setTimeout(function () {
            settime(obj)
    }, 1000);
}
function getCaptcha() {
    $.ajax({
        type: "POST",
        url: "/request-conv/index.php?m=Login&a=getVerify",
        dataType: 'json',
        success: function (result) {
            captcha=result;
            if(result.Code==200){
                $("#captcha_image").attr('src',result.captcha_image);
            }
        },
        error:function () {
            postStart=true;
        }
    });
}
function getMsg(obj) {
    obj.setAttribute("disabled", true);
    $.ajax({
        type: "POST",
        url: "/request-conv/index.php?m=Login&a=sendMessage",
        dataType: 'json',
        data: {
            phone : $("#tel").val(),
            verify  : $("#verify").val(),
            captcha_token : captcha.captcha_token
        },
        success: function (result) {
            obj.removeAttribute("disabled");
            delete captcha.is_user;
            if(result.Code==200) {
                settime(obj);
                captcha.is_user=result.is_user;
            }else if(result.Code==4005){
                getCaptcha();
                CommonJs.alert.show({
                    model: 'error',
                    content: result.erMsg
                });
            }else{
                captcha.captcha_token=result.captcha_token;
                captcha.captcha_image=result.captcha_image;
                $("#captcha_image").attr('src',result.captcha_image);
            }
        },
        error:function () {
            obj.removeAttribute("disabled");
        }
    });
}
$(function() {
    // 登陆分页栏
    $(".Hr_Login .nav li").click(function () {
        var index = $(this).index();
        $(this).addClass("active").siblings().removeClass("active");
        $(".nav-content").eq(index).addClass("shows").siblings().removeClass("shows");
        $(".error-field").hide();
    });
    getCaptcha();
    // $("#dymanic").click(function(){
    //     CommonJs.alert.show(Model_Date.Success[0]);
    // })
    var postStart=true;
    $("#login-submit").click(function (e) {
        e.preventDefault();
        if(postStart) {
            postStart=false;
            CommonJs.cookie.del('u_id');
            $.ajax({
                type: "POST",
                // url: "/request-conv/index.php?m=Login&a=checkLogin",
                url: "/request-conv/index.php?m=Login&a=userLogin",
                dataType: 'json',
                data: {
                    account: $("#account").val(),
                    password: $("input[name=password]").val(),
                    default_auto: $("#remember-me").prop('checked') ? true : ''
                },
                success: function (result) {
                    postStart=true;
                    if (result.Code == 200) {
                        if (window.location.href.indexOf('/login') >= 0) {
                            window.location.href = "/";
                        } else {
                            window.location.reload();
                        }
                    } else {
                        CommonJs.alert.show({
                            model: 'error',
                            content: result.erMsg
                        });
                    }
                },
                error:function () {
                    postStart=true;
                }
            });
        }
    });
    var postStart=true;
    $("#login-submit2").click(function (e) {
        e.preventDefault();
        if(postStart) {
            postStart=false;
            CommonJs.cookie.del('u_id');
            $.ajax({
                type: "POST",
                // url: "/request-conv/index.php?m=Login&a=checkLogin",
                url: "/request-conv/index.php?m=Login&a=checkPhoneLogin",
                dataType: 'json',
                data: {
                    is_user:captcha.is_user,
                    tel : $("#tel").val(),
                    dx_verify: $("#yz").val(),
                    captcha_token:captcha.captcha_token,
                    verify: $("#verify").val()
                },
                success: function (result) {
                    postStart=true;
                    if (result.Code == 200) {
                        if(captcha.is_user){
                            window.location.reload();
                        }else {
                            if (window.location.href.indexOf('/login') >= 0) {
                                window.location.href = "/";
                            } else {
                                window.location.reload();
                            }
                        }
                    } else {
                        CommonJs.alert.show({
                            model: 'error',
                            content: result.erMsg
                        });
                    }
                },
                error:function () {
                    postStart=true;
                }
            });
        }
    });
});